import { adminDemand } from '@/api/content.js'

export let adDemand = {
    namespaced: true,
    state: ()=>({
        demands: [],
        editDemand: {
            id: '',
            title: '',
            type: '',
            add_time: '',
            size: '',
            origin: '',
            price: '',
            rec: 0,
            update_time: '',
            detail_text: '',
            unit: '',
        },
        editID: -1,
        count: -1,
        currentTabs: 'data',
        loading: false,
        showEdit: false,
    }),
    mutations: {
        async getDemands(state, page) {
            if (state.loading)return
            state.loading = true
            let data = await adminDemand.getAll(page);
            state.loading = false
            state.demands = data.demands
            state.count = data.count
        },
        async setEditDemand(state, scope){
            state.editID = scope.index_;
            state.loading = true;
            let data = await adminDemand.getDemand(scope.row.id);
            state.editDemand = data.demand
            state.loading = false;
            state.showEdit = true;
            state.currentTabs = 'edit';
        },
        changeDemand(state, demand){
            state.demands[state.editID] = demand
            state.showEdit = false
            state.currentTabs = 'data'
        },
        unshiftDemand(state, demand){
            state.demands.unshift(demand)
        },
        spliceDemand(state, index){
            state.demands.splice(index, 1)
        },
        cancelEdit(state){
            state.editDemand = {
                id: '',
                title: '',
                type: '',
                add_time: '',
                size: '',
                origin: '',
                price: '',
                rec: 0,
                update_time: '',
                detail_text: '',
                unit: '',
            }
            state.editID = -1;
            state.currentTabs = 'data';
            state.showEdit = false ;
        },
        setLoading(state, flag){
            state.loading = flag
        }
        
    }
}